package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.sheet.pojo.dto.SheetAmountDetailGetDTO;
import com.xbongbong.pro.sheet.pojo.vo.SheetAmountDetailGetVO;
import com.xbongbong.saas.help.SaasPaymentHelper;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author zcp
 * @version v1.0
 * @date 19/11/13 013 17:34
 * @update 19/11/13 013 17:34
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/mobile/payment")
public class MobilePaymentController {

    @Resource
    private SaasPaymentHelper saasPaymentHelper;

    /**
     * 回款单金额明细
     * @param sheetAmountDetailGetDTO
     * @param br
     * @return java.lang.String
     * @author zcp
     * @date 19/11/12 012 16:10
     * @update 19/11/12 012 16:10
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/amount/detail/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String amountDetailGet(@RequestBody @Valid SheetAmountDetailGetDTO sheetAmountDetailGetDTO, BindingResult br) {
        XbbResponse<SheetAmountDetailGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SheetAmountDetailGetVO sheetAmountDetailGetVO = saasPaymentHelper.paymentAmountDetailGet(false, sheetAmountDetailGetDTO);
                response = new XbbResponse<>(sheetAmountDetailGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
